package com.example.xyxwebbackend001.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.xyxwebbackend001.config.FileUploadConfig;
import com.example.xyxwebbackend001.mapper.CityMapper;
import com.example.xyxwebbackend001.mapper.TPostsxMapper;
import com.example.xyxwebbackend001.mapper.USERMapper;
import com.example.xyxwebbackend001.mapper.UserInfoMapper;
import com.example.xyxwebbackend001.model.entity.TPosts;
import com.example.xyxwebbackend001.model.entity.User;
import com.example.xyxwebbackend001.model.entity.UserInfo;
import com.example.xyxwebbackend001.model.vo.PartUserInfoVO;
import com.example.xyxwebbackend001.model.vo.PartUserVO;
import com.example.xyxwebbackend001.model.vo.SecurityInfoVO;
import com.example.xyxwebbackend001.model.vo.UserProfileVO;
import com.example.xyxwebbackend001.service.UserCenterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;


@Service
public class UserCenterServiceImpl implements UserCenterService {
    @Autowired
    private USERMapper userMapper;

    @Autowired
    private FileUploadConfig fileUploadConfig;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private TPostsxMapper tPostsxMapper;

    @Override
    public PartUserInfoVO getPartUserInfo(Integer userId) {
        PartUserVO partUserVO = userMapper.getPartUserById(userId);
        String profile = userInfoMapper.getSignatureByUserId(userId);
        if (partUserVO != null) {
            PartUserInfoVO partUserInfoVO = new PartUserInfoVO();
            partUserInfoVO.setUserId(partUserVO.getUserId());
            partUserInfoVO.setUsername(partUserVO.getUsername());
            partUserInfoVO.setAvatarPath(partUserVO.getAvatarPath());
            partUserInfoVO.setSignature(profile);
            return partUserInfoVO;
        }
       return null;
    }

    @Override
    public UserProfileVO getUserProfile(Integer userId) {
        User user = userMapper.getUserById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        UserProfileVO vo = new UserProfileVO();
        BeanUtils.copyProperties(user, vo);
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUserId, userId);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        if (userInfo != null) {
            vo.setSex(userInfo.getSex());
            vo.setSignature(userInfo.getSignature());
            vo.setProfile(userInfo.getProfile());
            vo.setInterestedCulture(userInfo.getInterestedCulture());

            // 处理城市信息
            String cityId = userInfo.getCityId();
            if (StringUtils.hasText(cityId)) {
                vo.setCityName(cityMapper.selectById(cityId) != null ?
                        cityMapper.selectById(cityId).getCityName() : "");
            } else {
                vo.setCityName("");
            }
        }
        return vo;
    }

    @Override
    public String getUserAvatarPath(Integer userId) {
        return userMapper.getUserAvatarPath(userId);
    }

    @Override
    public String updateUserAvatar(Integer userId, MultipartFile file) throws IOException {
        String oldPath = userMapper.getUserAvatarPath(userId);
        String uploadAvatarDir = fileUploadConfig.getUploadAvatarDir() + '/' + userId + '/';
        String fileName = userId + "_" + System.currentTimeMillis() + "." + StringUtils.getFilenameExtension(file.getOriginalFilename());
        File dest = new File(uploadAvatarDir + fileName);
        file.transferTo(dest);

        String dbPath = fileName;
        userMapper.updateUserAvatar(userId, dbPath);

        if (StringUtils.hasText(oldPath)) {
            File oldFile = new File(uploadAvatarDir + oldPath);
            if (oldFile.exists()) {
                Files.delete(oldFile.toPath());
            }
        }

        return dbPath;

    }

    @Override
    public Integer updateUserProfile(Integer userId, String sex, String signature, String profile, String cityId, String interestedCulture) {
        return userInfoMapper.updateUserProfile(userId, sex, signature, profile, cityId, interestedCulture);
    }

    @Override
    public List<TPosts> getUserTPosts(Integer userId) {
        return tPostsxMapper.getPostsListByUserId(userId);
    }

    @Override
    public Integer deleteTPostById(Integer tPostId) {
        return tPostsxMapper.deletePostById(tPostId);
    }

    @Override
    public SecurityInfoVO getSecurityInfo(Integer userId) {
        return userMapper.getSecurityInfo(userId);
    }

    @Override
    public Integer updateEmail(Integer userId, String email) {
        return userMapper.updateEmail(userId, email);
    }

}
